multithreading - Update value after passing pointer -


i using tcp server send char array. function send() takes char *, but, before that, has listen , accept connection. given that, want send recent data when incoming connection accepted. previously, used 2 threads. 1 updated value in buffer, other waited connections, sent data.

i understand there can problems not locking mutex, aside that, same scheme work if passed char * send function, rather updating global variable?

some code demonstrate:

#include <pthread.h>  char buf[buflen];  void *updatebuffer(void *arg) {     while(true) {         getnewdata(buf);     } }  void *senddata(void *arg) {     //setup socket     while(true) {         newfd = accept(sockfd, (struct sockaddr *)&their_addr, &size);         send(newfd, buf, buflen, 0);         close(newfd);     } } 

this send updated values whenever new connection established.

i want try this:

#include <pthread.h>  char buf[buflen];  void *updatebuffer(void *arg) {     while(true) {         getnewdata(buf);     } }  void *senddata(void *arg) {     tcpserver tcpserver;     while(true) {         tcpserver.send(buf);    } } 

where function tcpserver.send(char *) same senddata() above.

the reason doing can make tcp server class, since i'll need use same code elsewhere.

from understanding, since passing pointer, it's same when call send(), since pass pointer there. value continue update, address won't change, should work. please let me know if correct. i'm open new ways of doing (without mutex locks, preferably).

yes, way of send, pass pointer buffer either void * or char *

i coded this:

int senddata(const char * buffer, const int length)  {     socket newfd;     int numofconnects=0;      while ((newfd=accept(sockfd, (struct sockaddr *)&their_addr, &size)) > 0)     {            // necessary here lock buffer mutex             send(newfd, buffer, length, 0);            // release mutex             close(newfd);             numofconnects++;     }      // there error in accept     // ok,      // if main thread has closed sockfd socket indicating quit.      // returns number of transfers have done.     return numofconnects; } 

one thing consider using pointer buffer modify in thread;

could in middle of send buffer changes , data sent not accurate.

situation you've noticed well. using mutex suggested indicated.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -