Hi all.
I have a socket. I want to create two threads, one is to read from socket, and another is to write to socket. However, I want these two threads to be able to share the same data, so I created a mutex to lock the resource.
I don't know what happens with my two threads, but it always quits after it did some tasks. -
#include "camel.h"
-
#include "irc.h"
-
-
pthread_t gtid;
-
pthread_mutex_t gMutex;
-
-
// The shared resource between reading and writing thread
-
char gSntBuffer[MAX_SIZE_BUFFER];
-
-
void*
-
SendRequest( INPUT void* pArg)
-
{
-
int lsockfd;
-
DBGSTR(3,
-
STATUS_SUCCESS,
-
"FUNCTION ENTRY",
-
"")
-
lsockfd = (int)pArg;
-
while(1)
-
{
-
pthread_mutex_lock(&gMutex);
-
-
if (gSntBuffer)
-
{
-
#ifdef DBG
-
printf("Snt Buffer : %s\n", gSntBuffer);
-
#endif
-
write(lsockfd, gSntBuffer, MAX_SIZE_BUFFER);
-
}
-
-
pthread_mutex_unlock(&gMutex);
-
}
-
DBGSTR(3,
-
STATUS_SUCCESS,
-
"FUNCTION EXIT",
-
"")
-
}
-
-
void*
-
ReceiveReply( INPUT void* pArg)
-
{
-
int lsockfd = (int)pArg;
-
char lRcvBuffer[MAX_SIZE_BUFFER];
-
int lBytes = 0;
-
DBGSTR(3,
-
STATUS_SUCCESS,
-
"FUNCTION ENTRY",
-
"")
-
while(1)
-
{
-
lBytes = read(lsockfd, lRcvBuffer, MAX_SIZE_BUFFER);
-
#ifdef DBG
-
printf("Rcv Buffer : %s", lRcvBuffer);
-
#endif
-
// here I have to filter the lRcvBuffer and
-
// then write something back to socket by filling
-
// the gSntBuffer only and let the SendRequest()
-
// do the rest of the job.
-
//
-
-
// .... CODE HERE
-
}
-
DBGSTR(3,
-
STATUS_SUCCESS,
-
"FUNCTION EXIT",
-
"")
-
}
-
-
tRtnCode
-
ConnectIRC( INPUT tProfile profile )
-
{
-
int lsockfd;
-
struct sockaddr_in lservaddr;
-
int lrvalue;
-
-
pthread_attr_t lattr;
-
-
DBGSTR(3,
-
STATUS_SUCCESS,
-
"FUNCTION ENTRY",
-
"")
-
-
memset(&lservaddr, 0, sizeof(lservaddr));
-
lservaddr.sin_family = AF_INET;
-
lservaddr.sin_port = htons(profile.port);
-
lservaddr.sin_addr.s_addr = inet_addr(profile.server);
-
-
if (0 >= (lsockfd = socket(AF_INET, SOCK_STREAM,0)))
-
{
-
DBGINT(1,
-
ERROR_SOCK_CANNOT_CREATE,
-
"lsockfd = ",
-
lsockfd)
-
return (ERROR_SOCK_CANNOT_CREATE);
-
}
-
-
printf("SendRequest() : losckfd = %d\n", lsockfd);
-
-
if (0 < (lrvalue = connect(lsockfd, (struct sockaddr*)&lservaddr, sizeof(lservaddr))))
-
{
-
DBGINT(1,
-
ERROR_SOCK_CANNOT_CONNECT,
-
"connect() returns ",
-
lrvalue)
-
return (ERROR_SOCK_CANNOT_CONNECT);
-
}
-
-
// set attributes
-
pthread_attr_init(&lattr);
-
pthread_attr_setdetachstate(&lattr, PTHREAD_CREATE_DETACHED);
-
pthread_attr_setscope(&lattr, PTHREAD_SCOPE_SYSTEM);
-
-
// init mutex
-
pthread_mutex_init(gMutex, NULL);
-
pthread_mutex_lock(&gMutex);
-
-
snprintf(gSntBuffer,
-
MAX_SIZE_BUFFER,
-
IRC_USER,
-
profile.username,
-
"",
-
"",
-
profile.fullname,
-
profile.nickname);
-
-
pthread_mutex_unlock(&gMutex);
-
-
// create 2 threads
-
// - read data
-
// - write data
-
-
pthread_create(>id, &lattr, SendRequest, (void*)lsockfd);
-
pthread_create(>id, &lattr, ReceiveReply, (void*)lsockfd);
-
-
DBGSTR(3,
-
STATUS_SUCCESS,
-
"FUNCTION EXIT",
-
"")
-
}
5 7302 Banfa 9,065
Recognized Expert Moderator Expert
It's not in the posted code but you need to ensure that main does not exit (i.e. return) until your threads have finished.
gpraghuram 1,275
Recognized Expert Top Contributor
HI,
R u creating the thread a sdetached or attached thread?
If you are creating it as a attcahed thred the call pthread_join() and return after the call.
Raghu
Thank gpraghuram
oh yes....you're right. I forget to set the attribute to be JOINABLE, and join them to the main thread, so that the main thread could wait for all of the child threads to be terminated.
Anyway, how about the same resource shared by two threads ?
I have to use the pthread_mutex_t to lock it right ?
I am not sure when to lock the resource. Let's say, there are 2 threads, one is to write data, and another is to read data, so in either reading or writing, we lock the resource ? or we lock only when we write ?
Banfa 9,065
Recognized Expert Moderator Expert
Lock for both reading and writing but for efficiency keep the lock for as short a time as possible.
If you don't lock for reading the writing thread may change the data in the middle of you reading it which could produce very strange results.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: lokb |
last post by:
Hi,
I am creating a detach thread as shown below and caling pthread_create in
a while loop where the file names in the directory are fetched and is
passed as a parmater to pthread create. The apilcation is running fine
except for the sleep cycle.
My sleep cylce is sleep(5) which is halting all the threads created from
execution. What i need here is the main thread creating the pthread should
only sleep but not all the threads created.
Is...
|
by: stefvienna |
last post by:
Hi group,
I'm trying compiling Python-2.4.1 and it fails builing the _socket
extension with the following message:
building '_socket' extension
gcc -pthread -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC
-fno-strict-aliasing -I. -I/usr/local/src/Python-2.4.1/./Include
-I/usr/local/include -I/usr/local/src/Python-2.4.1/Include
-I/usr/local/src/Python-2.4.1 -c
|
by: nan.li.g |
last post by:
Hello, all,
I have an interesting problem about stl map and pthread on Linux
and g++. The source code is as follows.
//mt_map_test.cpp
#include <string>
#include <map>
#include <unistd.h>
#include <sys/types.h>
|
by: david wolf |
last post by:
The following pthread code cannot compile, I want to know why. Can
somebody give me direction on this?
if I replace the following line of code
status1 = pthread_create(&threadID1, NULL, read, NULL);
with:
status1 = pthread_create(&threadID1, NULL, readerWriter::read, NULL);
|
by: kanchan |
last post by:
Hi, I am writing a client for a real time audio streaming server. I am
using pthread for that. I have one thread receiving data on a socket from
the server. After buffering some data, other thread wakes up (checking how
much data is in the buffer) to play the music. The problem is that when the
2nd thread is playing, the first thread is not able to receive data from
the server. If the server sends the data slow, then there will be breaks...
| |
by: Yim |
last post by:
In below codes,
After 10 seconds, function t() was called. So far everything is ok.
Then I want to awake blocked read(). So want to exit program.
In t(), how to do?
(in t(), close(sockfd) don't awake read());
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
|
by: noleander |
last post by:
I've got an application that uses Pthread to do threading. Mostly Im using
Condition Variables and the associated function calls:
- pthread_cond_wait()
- pthread_cond_signal()
- pthread_cond_broadcast()
My boss says we cant use Pthread because of licensing issues (LGPL) .. we
can only use Native Win32 threading/mutex functions.
|
by: Sean |
last post by:
Hi,
I am trying to write a simple chat/text messaging program but I am
having some problems. I am a rookie when it comes to socket programming
so I am not sure if I am doing the write thing or not. Here is my code:
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
|
by: Stuart |
last post by:
I am in the process of teaching myself socket programming. I am
"playing around" with
some simple echo server-client programs for m the book TCP/IP Sockets
in C.
The Server program is:
#include "TCPEchoServer.h" /* TCP echo server includes */
#include <pthread.h /* for POSIX threads */
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |