Solved

Sending between threads

Posted on 2004-03-20
21
182 Views
Last Modified: 2010-04-01
I have a simple multi threaded program, a TCP server, and i am tryinng to send data between each thread, for example sending data to each thread's socket. Basicly the sort of thing i need to to be able to somehow find every thread thats running and be able to access variables in it (for example ints and chars, and sockets of course) is this even possible? and if so, how would i go about it?

I have managed to find a little work around by having each thread assigned a number then using several CStrinngLists's and thread 4 for example uses the fourth item on each list - that way the variables are stored in the lists which are global so a thread can easilly access anouther threads variables by just going to the wrong place on the list and doing stuff, but this is unreliable and not really a good way to do it i dont think.
0
Comment
Question by:SnipKing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 4
  • 2
21 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10641980
>>i need to to be able to somehow find every thread thats running and be able to access
>>variables in it (for example ints and chars, and sockets of course) is this even possible?

Have your threads use a common storage layout like

class CThreadData {

public:

    CThreadData () { InitializeCriticalSection ( &m_cs);}
    ~CThreadData () { DeleteCriticalSection ( &m_cs);}

    int GetSomeDate () {
        int n;
        EnterCriticalSection ( &m_cs);
        n = m_nSomeData;
        LeaveCriticalSection ( &m_cs);
        return n;
    }
    void SetSomeDate ( int n) {
        EnterCriticalSection ( &m_cs);
        m_nSomeData = n;
        LeaveCriticalSection ( &m_cs);
    }
protected:
    int m_nSomeData;
    CRITICAL_SECITON m_cs;
};

This will ensure the proper synchronisation when you access the data. You can keep these classes in a list and pass a pointer to each thread's associated instance in the 1st parameter when creating it.
0
 

Author Comment

by:SnipKing
ID: 10642017
would this allow me to use sockets on other threads? or just have a variable that all threads can access? (like a global variable)
0
 
LVL 86

Expert Comment

by:jkr
ID: 10642053
You will have a common storage structure, one for each thread. Whether you put sockets in there or other data is up to you.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SnipKing
ID: 10642219
but how would i access the other threads variables? how would i reference the thread?
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 10642231
>>how would i reference the thread?

If you have created a thread, pass it a pointer to the struct. Then, you would use the Thread ID to access the associated struct. Just put it in a map or something else...
0
 
LVL 30

Expert Comment

by:Axter
ID: 10644362
You can share data between threads by using MapView API functions.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10644365
0
 
LVL 86

Expert Comment

by:jkr
ID: 10644370
>>You can share data between threads by using MapView API functions

Shouldn't that be "between processes"? For threads, this is overkill, since they already share a common address space.
0
 
LVL 86

Expert Comment

by:jkr
ID: 10644372
Um, the article reads "for exchanging data between executables"...
0
 
LVL 30

Expert Comment

by:Axter
ID: 10644548
>>Shouldn't that be "between processes"? For threads, this is overkill, since they already share a common address space.
You're right.
It's Sunday morning, and I didn't have my morning coffee yet. :-)
0
 
LVL 86

Expert Comment

by:jkr
ID: 10644555
>>It's Sunday morning, and I didn't have my morning coffee yet

Well, here in NYC, too - but I *did* already have coffe :o)
0
 
LVL 86

Expert Comment

by:jkr
ID: 10688886
SnipKing - anything else you need to know?
0
 
LVL 86

Expert Comment

by:jkr
ID: 10939298
Um, what Axter posted applies to inter-process communication, not inter-thread communication.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10939366
I agree.
0
 
LVL 86

Expert Comment

by:jkr
ID: 10968638
modulo - do you read the comments that we are expected to leave before forcing an "accept"? :o)
0
 
LVL 86

Expert Comment

by:jkr
ID: 10969095
>>but saw no new recommandation in your post

Well, I thought I was clear in stating that Axter's comments were not really related to the subject of this Q and he agreed...
0
 
LVL 86

Expert Comment

by:jkr
ID: 10969804
Thanks, modulo - I'll try to be less unclear the next time :o)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question