I am working for a client-server program using C language for Windows. Here I have to create 5 threads that will run concurrently and as soon as the client is connected, the free thread among 5 will interact with client. If none-of-the 5 threads are free the client will be prompted "Server busy try later... ". AS I am the newbie for multithreading, please suggest some solutions. Thankss.
(Requirement: The threads should be created either with _beginthread() or _beginthreadex()).
Related
I am creating a web service that creates a huge amount of small java timer threads over (10k). I can only seem to create 2k timer threads before I get the OutOfMemoryError: unable to create new native thread. How do i solve this? I am using a macbook pro to run my Tomcat server on. I'v configured the ulimit (-u) max user processes to double what it used to be but I still get the same problem. What are my options, if any, to make this doable?
It's often a bad idea for web applications to start their own (few) threads, let alone 10K threads - and then "as timers"? Seriously? Don't go there.
What can you do?
Don't rely on the ability to create those threads.
Change your architecture! Use a scheduler library that has solved this problem already (e.g. Quartz or others).
If you don't want to use an external library (why wouldn't you?): Implement a single timer thread that executes the scheduled operations when they're due. Do not use a new thread for each scheduled operation
If you wanted to boil 100 eggs, would you buy 100 timers?
I have the following strange situation.
We have a process, call it Distributor, that receives tasks over ZeroMQ/TCP from Client, and accumulates them in a queue. There is a Worker process, which talks with the Distributor over ZeroMQ/IPC. The Distributor forwards each incoming task to Worker, and waits for an answer. As soon as the Worker answers, it sends it another task (if there was one received in the mean time), and returns the answer to the Client (over a separate ZeroMQ/TCP connection). If a task was not processed within 10ms, it is dropped from the queue.
With 1 Worker, the system is capable to process ~3,500 requests/sec. The client sends 10,000 requests/sec, so 6,500 requests are dropped.
But - when I'm running some unrelated process on the server, which takes 100% CPU (a busy wait loop, or whatever) - then, strangely, the system can suddenly process ~7,000 requests/sec. When the process is stopped, it returns back to 3,500. The server has 4 cores.
The same happens when running 2, 3 or 4 Workers (connected to the same Distributor), with slightly different numbers.
The Distributor is written in C++. The Worker is written in Python, and uses pyzmq binding. The worker process is a simple arithmetic process, and does not depend on any external I/O other than Distributor.
There is a theory that this has to do with ZeroMQ using threads on separate CPUs when the server is free, and the same CPU when it's busy. If this is the case, I would appreciate an idea how to configure thread/CPU affinity of ZeroMQ so that it works correctly (without running a busy loop in background).
Is there any ZeroMQ setting that might explain / fix this?
EDIT:
This doesn't happen with a Worker written in C++.
This was indeed a CPU affinity problem. Turns out that using ZeroMQ in a setting where a worker processes an input and waits for the next one, if the context switch causes it to switch to another process, a lot of time is wasted on copying the ZeroMQ data.
Running the worker with
taskset -c 1 python worker.py
solves the problem.
i am working on mac application development,need help regarding connecting to remote system asynchronously with parallel execution.
How i can create 5 parallel threads in COCOA programming(at run time need to create based on count i.e 10 threads may be),
Need to connect to 5 MAC machines at a time based on thread count.
Please Guide me with SSH Wrapper and parallel execution of thread in COCOA(Mac) programming.
Thanks,
Shabana
Background:
I have a C#.Net (.Net 4.0) Website that calls a engine to grab data from an external database through a vendor executable file that I cannot modify. Whenever user click on a specific button, the webpage will instruct the engine to spawn a few threads, and each thread will spawn a process of the executable (let's called it ABC.exe) to grab the data. The executable will then run, and save the data grabbed into a CSV file on the server. The threads then read the CSV first and consolidate all the data, do some calculation and return to the website.
Problem:
When we deployed the website to IIS 7.5 on a web server (running Window 7, four virtual processors), if we spawn more than 4 threads, the processes will just stuck there.
Test done:
When we run the exact codes using Visual Studio on the same server, no error occurred. All the threads are spawned correctly, and all process is running correctly. This is very weird.
When two user click on the button and both click will spawn 4 threads, the first four threads will work correctly while the second four threads will just stuck there.
When we run the thread with different executables that do not perform any connections, the code works.
In our code, using Task or Thread doesn't make a difference.
We suspect that this might have something to do with the outbound connections allowed to a specific IP in IIS.
Can anyone shed some light on this? Let me know if any further information is required. Thanks!
A couple of thoughts:
Could this be tied to thread pool threads? The CLR tries to prevent the CPU from unnecessary spinning by not handing out (initially anyways) more threads on the thread pool thread than there are processors present. Are there 4 virtual processors present? Thread pool threads should resume eventually, though, so make sure that your threads are really stuck, and not running serial.
I know of a deadlock condition when redirecting standard output in C#. Double check the MSDN article to make sure this is not happening to you.
Additional thoughts:
At this point, it sounds like your ABC.exe is suspect. Since you have Visual Studio on the server, I recommend that you fire up a separate instance of Visual Studio, and attach to one of the ABC.exe processes to see where it is hanging. Also try to run something that you are reasonably sure would exit, e.g. cmd /c dir instead of your ABC.exe.
Edit: 5/29:
I find it hard to believe that IIS would restrict outbound connections this way. Try the following simple downloaded instead of ABC.exe:
class Program
{
public static void Main(string[] args)
{
using (var reader = new System.IO.StreamReader(
System.Net.HttpWebRequest.Create("http://www.google.com")
.GetResponse().GetResponseStream()))
{
System.Console.WriteLine(reader.ReadToEnd());
}
}
}
I know the term "Load Balancing" can be very broad, but the subject I'm trying to explain is more specific, and I don't know the proper terminology. What I'm building is a set of Server/Client applications. The server needs to be able to handle a massive amount of data transfer, as well as client connections, so I started looking into multi-threading.
There's essentially 3 ways I can see implementing any sort of threading for the server...
One thread handling all requests (defeats the purpose of a thread if 500 clients are logged in)
One thread per user (which is risky to create 1 thread for each of the 500 clients)
Pool of threads which divide the work evenly for any number of clients (What I'm seeking)
The third one is what I'd like to know. This consists of a setup like this:
Maximum 250 threads running at once
500 clients will not create 500 threads, but share the 250
A Queue of requests will be pending to be passed into a thread
A thread is not tied down to a client, and vice-versa
Server decides which thread to send a request to based on activity (load balance)
I'm currently not seeking any code quite yet, but information on how a setup like this works, and preferably a tutorial to accomplish this in Delphi (XE2). Even a proper word or name to put on this subject would be sufficient so I can do the searching myself.
EDIT
I found it necessary to explain a little about what this will be used for. I will be streaming both commands and images, there will be a double-socket setup where there's one "Main Command Socket" and another "Add-on Image Streaming Socket". So really one connection is 2 socket connections.
Each connection to the server's main socket creates (or re-uses) an object representing all the data needed for that connection, including threads, images, settings, etc. For every connection to the main socket, a streaming socket is also connected. It's not always streaming images, but the command socket is always ready.
The point is that I already have a threading mechanism in my current setup (1 thread per session object) and I'd like to shift that over to a pool-like multithreading environment. The two connections together require a higher-level control over these threads, and I can't rely on something like Indy to keep these synchronized, I'd rather know how things are working than to learn to trust something else to do the work for me.
IOCP server. It's the only high-performance solution. It's essentially asynchronous in user mode, ('overlapped I/O in M$-speak), a pool of threads issue WSARecv, WSASend, AcceptEx calls and then all wait on an IOCP queue for completion records. When something useful happens, a kernel threadpool performs the actual I/O and then queues up the completion records.
You need at least a buffer class and socket class, (and probably others for high-performance - objectPool and pooledObject classes so you can make socket and buffer pools).
500 threads may not be an issue on a server class computer. A blocking TCP thread doesn't do much while it's waiting for the server to respond.
There's nothing stopping you from creating some type of work queue on the server side, served by a limited size pool of threads. A simple thread-safe TList works great as a queue, and you can easily put a message handler on each server thread for notifications.
Still, at some point you may have too much work, or too many threads, for the server to handle. This is usually handled by adding another application server.
To ensure scalability, code for the idea of multiple servers, and you can keep scaling by adding hardware.
There may be some reason to limit the number of actual work threads, such as limiting lock contention on a database, or something similar, however, in general, you distribute work by adding threads, and let the hardware (CPU, redirector, switch, NAS, etc.) schedule the load.
Your implementation is completely tied to the communications components you use. If you use Indy, or anything based on Indy, it is one thread per connection - period! There is no way to change this. Indy will scale to 100's of connections, but not 1000's. Your best hope to use thread pools with your communications components is IOCP, but here your choices are limited by the lack of third-party components. I have done all the investigation before and you can see my question at stackoverflow.com/questions/7150093/scalable-delphi-tcp-server-implementation.
I have a fully working distributed development framework (threading and comms) that has been used in production for over 3 years now across more than a half-dozen separate systems and basically covers everything you have asked so far. The code can be found on the web as well.