Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void* WorkerThread(void* args)
- {
- DoSomeImportantInitialization(); // initialize listen socket and some thread specific stuff
- while (HasExitConditionBeenSet()==false)
- {
- listensize = sizeof(listenaddr);
- int sock = accept(listensocket, &listenaddr, &listensize);
- // check if exit condition has been set using thread safe semantics
- if (HasExitConditionBeenSet())
- {
- break;
- }
- if (sock < 0)
- {
- printf("accept returned %d (errno==%d)n", sock, errno);
- }
- else
- {
- HandleNewNetworkCondition(sock, &listenaddr);
- }
- }
- DoSomeImportantCleanup(); // close listen socket, close connections, cleanup etc..
- return NULL;
- }
- void SignalHandler(int sig)
- {
- printf("Caught CTRL-Cn");
- }
- void NotifyWorkerThreadToExit(pthread_t thread_handle)
- {
- // signal thread to exit
- }
- int main()
- {
- void* ptr_ret= NULL;
- pthread_t workerthread_handle = 0;
- pthread_create(&workerthread, NULL, WorkerThread, NULL);
- signal(SIGINT, SignalHandler);
- sleep((unsigned int)-1); // sleep until the user hits ctrl-c
- printf("Returned from sleep call...n");
- SetThreadExitCondition(); // sets global variable with barrier that worker thread checks on
- // this is the function I'm stalled on writing
- NotifyWorkerThreadToExit(workerthread_handle);
- // wait for thread to exit cleanly
- pthread_join(workerthread_handle, &ptr_ret);
- DoProcessCleanupStuff();
- }
- // NotifyPipe.h
- #ifndef NOTIFYPIPE_H_INCLUDED
- #define NOTIFYPIPE_H_INCLUDED
- class NotifyPipe
- {
- int m_receiveFd;
- int m_sendFd;
- public:
- NotifyPipe();
- virtual ~NotifyPipe();
- int receiverFd();
- void notify();
- };
- #endif // NOTIFYPIPE_H_INCLUDED
- // NotifyPipe.cpp
- #include "NotifyPipe.h"
- #include <unistd.h>
- #include <assert.h>
- #include <fcntl.h>
- NotifyPipe::NotifyPipe()
- {
- int pipefd[2];
- int ret = pipe(pipefd);
- assert(ret == 0); // For real usage put proper check here
- m_receiveFd = pipefd[0];
- m_sendFd = pipefd[1];
- fcntl(m_sendFd,F_SETFL,O_NONBLOCK);
- }
- NotifyPipe::~NotifyPipe()
- {
- close(m_sendFd);
- close(m_receiveFd);
- }
- int NotifyPipe::receiverFd()
- {
- return m_receiveFd;
- }
- void NotifyPipe::notify()
- {
- write(m_sendFd,"1",1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement