Skip to main content

Posts

Showing posts with the label Multi-thread

Threaded multi Chat room client source code in c++ with MFC Part - 3

PREVIOUS ClientDlg.cpp is the class where all the action from user's are taken and initiated. The interaction with GUI to logic is so great and easy which will help any developer to understand the flow of code. Function for login -- void CClientDlg::OnBnClickedButton2() {     // TODO: Add your control notification handler code here     cTh = AfxBeginThread(     StaticThreadFunc,     this);         m_Thread_handle = cTh->m_hThread; } Start the thread for a login action. UINT __cdecl CClientDlg::StaticThreadFunc(LPVOID pParam) {     CClientDlg *pYourClass = reinterpret_cast<CClientDlg*>(pParam);     UINT retCode = pYourClass->ThreadFunc();     return retCode; } UINT CClientDlg::ThreadFunc() {     // Do your thing, this thread now has access to all the classes member variables     ...

Threaded multi Chat room client source code in c++ with MFC Part - 2

PREVIOUS                                                                                                                                NEXT Client.cpp class is the MFC GUI initiation class. You can have a look here .. BOOL CClientApp::InitInstance() {     // InitCommonControlsEx() is required on Windows XP if an application   ...

Threaded multi Chat room client source code in c++ with MFC Part - 1

As in my previous post I have talk about server , now it's time for client. This client will use as multi chat client with our very own server. In this client you need to provide server IP , PORT and an USERNAME . Then just click the log in button and you are in if you get an welcome message from server. Technology used: i.   C++ ii.   Microsoft visual Studio 2010 iii.  MFC GUI iv.  Winshock2 Socket v.   TCP/UDP vi.  Windows Thread This project consist of  3 classes. 1. Client.cpp 2. ClientDlg.cpp 3. ClientCon.cpp Lets start with core portion -- I mean the network level connection portion. I have used the Winshock2 library for the purpose of network communication. You can use this library on windows  with MVS 2010 by including library -- # include <winsock2.h> And as it is multi user  and have GUI so we need thread. For this I have used Windows thread. You can use this windows thr...

Threaded multi chat room server source code in c++ with MFC Part - 3

PREVIOUS   Now we will talk about the other MFC GUI portion which I have created and used threaded model. When in GUI the start button is clicked by providing port number on PORT text box this server start by creating a thread independent from GUI. You can start the server by ENTER also. Here is some code snap for help -- void CServerDlg::OnBnClickedButton1() {     // TODO: Add your control notification handler code here     cTh = AfxBeginThread(     StaticThreadFunc,     this);         //cTh->m_bAutoDelete = FALSE;     m_Thread_handle = cTh->m_hThread;     } Here we create a windows thread for back end action. UINT __cdecl CServerDlg::StaticThreadFunc(LPVOID pParam) {     CServerDlg *pYourClass = reinterpret_cast<CServerDlg*>(pParam);     UINT retCode = pYourClass->ThreadFunc(); ...

Threaded multi chat room server source code in c++ with MFC Part - 2

PREVIOUS  we will now demonstrate about the class Server.cpp . This class is the start point of the server project. It is basically used to call the MFC instance. There will be some preview of the class here as MFC is out of scope  at the moment -- BOOL CServerApp::InitInstance() {     // InitCommonControlsEx() is required on Windows XP if an application     // manifest specifies use of ComCtl32.dll version 6 or later to enable     // visual styles.  Otherwise, any window creation will fail.     INITCOMMONCONTROLSEX InitCtrls;     InitCtrls.dwSize = sizeof(InitCtrls);     // Set this to include all the common control classes you want to use     // in your application.     InitCtrls.dwICC = ICC_WIN95_CLASSES;     InitCommonControlsEx(&InitCtrls);     CWinApp::InitInstance();   ...

Threaded multi chat room server source code in c++ with MFC Part - 1

Multi-client chat server is open source as I have created it. In this server multiple user can log in and communicate with one another as multicast user. In reality it can use as a chat room server. Can be modified as peer to peer (p2p) text chat. It can also be used as a backbone of a video conferencing project. It has a beautiful and easy to use user interface in MFC. This project is compatible with Microsoft visual studio 2010. I will describe the use of class and function with source code. Technology used: i.   C++ ii.   Microsoft visual Studio 2010 iii.  MFC GUI iv.  Winshock2 Socket v.   TCP/UDP vi.  Windows Thread This project consist of  3 classes. 1. Server.cpp 2. ServerDlg.cpp 3. ServerManager.cpp Lets start with core portion -- I mean the network level connection portion. I have used the Winshock2 library for the purpose of network communication. You can use this library on windows  with MVS 2...

GUI hanged on button pressed or some action initiation in MFC project

ISSUE: In a GUI project there is lots of calculation and works are needed to be done in background when necessary . So when a button pressed or a certain action initiate the GUI may stop responding for you. SOLUTION: In default when a MFC project is created the GUI portion has thread. So make sure the other necessary works are done on another thread (eg: calculation, process, response , internet query ). You can have a better and great idea about thread use in mfc from this simple post

Best design for windows multi thread in a c++ class

ISSUE: How to create windows multi-threaded program in  c++ ? SOLUTION:   Now a day maximum project or software has multiple process running at a same process.Or a gui application needs to do some work on background which is blocking. Or may be you are going to design or create a network application ,so it becomes mandatory to be an expert of Threading now a days. Here is  the template which I follow. In header file add the followings -- #include <Windows.h> static UINT __cdecl CallingFunctionForThread ( LPVOID pParam ); UINT WorkingFunction ();   In CPP file add the followings -- UINT __cdecl My Class :: CallingFunctionForThread ( LPVOID pParam ) { My Class * pMy Class = reinterpret_cast < My Class *>( pParam ); UINT ret = pMy Class -> WorkingFunction (); return ret ; } UINT My Class :: WorkingFunction () { // You can do anything as a class member function and run it on your thread. } Start ...

Multithreading in C in Different CPU core

# define _GNU_SOURCE # include <stdio.h> # include <math.h> # include <stdlib.h> # include <pthread.h> # include <stdlib.h> pthread_mutex_t mux = PTHREAD_MUTEX_INITIALIZER; struct p2p{ int st; int en; }; void *primeChk(void* val); int main() {   pthread_t thread1,thread2,thread3,thread4;   struct p2p valu1,valu2,valu3,valu4;   int j,ret1,ret2,ret3,ret4;   cpu_set_t cpuset; // pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);   valu1.st = 1;   valu1.en = 5000;   valu2.st = 5001;   valu2.en = 10000;   valu3.st = 10001;   valu3.en = 15000;   valu4.st = 15001;   valu4.en = 20000;    // initialization    CPU_ZERO(&cpuset);    CPU_SET(0,&cpuset);    pthread_setaffinity_np(thread1, sizeof(cpu_set_t), &cpuset);    ret1 = pthread_create(&thread1,NULL,&primeChk,(void*)&valu1);    //CPU_ZERO(&cpuse...