Adrian
Adrian

Reputation: 20068

Using posix semphores question

I am curios if i implement this correct using semaphores.I want that clientThread to execute only after serverThread finish.
My current code gives me an error(not compile/linker error) with "Close program"(only if i print in clientThread) Here is my code:

void* serverThread(void* sock)
{
    sem_wait(&mutex);
    Handle handle((TCPSocket*)sock);
    handle.HandleAuthentication();
    sem_post(&mutex);
    sem_post(&mutex2);
}

void* clientThread(void* sock)
{
    sem_wait(&mutex2);
    cout << "Address is: -> " << Handle::getAddress() << " ------ " << Handle::getPort() << endl;
    sem_post(&mutex2);
}

int main()
{
    sem_init(&mutex, 0, 1);
    sem_init(&mutex2, 0, 0);
    unsigned short echoServPort = 9898;

    try {
        TCPServerSocket servSock(echoServPort);

        for (;;) {  
            TCPSocket* sock = servSock.accept();
            pthread_t pidClient, pidServer;
            if(pthread_create(&pidServer, NULL, serverThread, (void*)sock) != 0)
            {
                throw SocketException("Unable to create server thread (pthread_create()", true);
            }
            if(pthread_create(&pidClient, NULL, clientThread, NULL) != 0)
            {
                throw SocketException("Unable to create client thread (pthread_create()", true);
            }
        }
    } catch (SocketException &e) {
        cerr << e.what() << endl;
        exit(1);
    }
    return 0;
}

edit: This are the errors after inspecting with gdb:

(gdb) bt
#0  0x6fcb6afb in libstdc++-6!_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES
5_PKc () from c:\MinGW\bin\libstdc++-6.dll
#1  0x0040146d in _fu0___ZSt4cout (sock=0x0) at ..\src\client.cpp:212
#2  0x6e0c5121 in ptw32_threadStart@4 () from c:\MinGW\bin\libpthread-2.dll
#3  0x76e01287 in msvcrt!_itow_s () from C:\Windows\system32\msvcrt.dll
#4  0x76e01328 in msvcrt!_endthreadex () from C:\Windows\system32\msvcrt.dll
#5  0x76cb1174 in KERNEL32!AcquireSRWLockExclusive ()
   from C:\Windows\system32\kernel32.dll
#6  0x76efb3f5 in ntdll!RtlInsertElementGenericTableAvl ()
   from C:\Windows\system32\ntdll.dll
#7  0x76efb3c8 in ntdll!RtlInsertElementGenericTableAvl ()
   from C:\Windows\system32\ntdll.dll
#8  0x00000000 in ?? ()

Upvotes: 0

Views: 199

Answers (1)

ssmir
ssmir

Reputation: 1542

I see a loop in your code so you probably meant something like:

void* serverThread(void* sock)
{
    sem_wait(&empty);
    Handle handle((TCPSocket*)sock);
    handle.HandleAuthentication();
    sem_post(&full);
}

void* clientThread(void* sock)
{
    sem_wait(&full);
    cout << "Address is: -> " << Handle::getAddress() << " ------ " << Handle::getPort() << endl;
    sem_post(&empty);
}

int main()
{
    sem_init(&full, 0, 0);
    sem_init(&empty, 0, 1);
    ...

If you program crashes then it would be better to compile it with -O0 -g and run under gdb to inspect the stack trace.

Or you can do ulimit -c unlimited in a shell so that core file will be created automatically.

Upvotes: 1

Related Questions